//
//  FoodNameTableViewController.m
//  DeliciousFoodBible
//
//  Created by lanou3g on 15/7/19.
//  Copyright (c) 2015年 孙连爽. All rights reserved.
//

#import "FoodNameTableViewController.h"
#import "FoodNameTableViewCell.h"
#import "detailTableViewController.h"
#import "FoodDetail.h"
#import "DetailCell.h"
#import "UIImageView+WebCache.h"
#import "ImageDownLoader.h"

@interface FoodNameTableViewController ()

@property (nonatomic,copy)   FoodDetail *food;
@property (nonatomic,copy)   NSArray *dataArray;
@property (nonatomic,strong) NSMutableArray *urlArray;
@property (nonatomic,strong) FoodDetail *foodList;
@property (nonatomic,strong) FoodDetail *foodDetail;
@property (nonatomic,strong) FoodNameTableViewCell *cell;
@property (nonatomic,strong) NSMutableArray *urlDetailArray;

@end

@implementation FoodNameTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FoodNameTableViewCell" bundle:nil] forCellReuseIdentifier:@"reuseIdentifier"];
    
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"foodDetail1_background.jpg"]];
    
    self.title = self.titleStr;
    [self dataRequestAndAnalysis];
   

    
}

//数据请求与解析
-(void)dataRequestAndAnalysis{
    
    //拼接接口
    NSString *string1 = @"http://apis.juhe.cn/cook/index?key=a8be7fa328b601bbb1e55ef1084f9bbb&cid=";
    NSString *string = [NSString stringWithFormat:@"%@%@",string1,self.urlID];
    
    //网络请求
    NSURL *url = [[NSURL alloc] initWithString:string];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLResponse *resopne = nil;
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&resopne error:&error];
    
    //数据解析
    _food = [[FoodDetail alloc] init];
    _food.foodNameDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    NSDictionary *resultDic = [_food.foodNameDic objectForKey:@"result"];
    self.dataArray = [resultDic objectForKey:@"data"];
    
    self.urlArray =[[NSMutableArray alloc] init];
    self.urlDetailArray = [[NSMutableArray alloc] init];
    for (NSDictionary *dic in self.dataArray) {

        self.foodList = [[FoodDetail alloc] init];

        NSArray *array = [dic objectForKey:@"albums"];
        
        self.foodList.image_url = array[0];
        [self.urlDetailArray addObject:self.foodList.image_url];
        [self.urlArray addObject:self.foodList];
    }
    
    [self.tableView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 155;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DetailTableViewController *detailVC = [[DetailTableViewController alloc] init];
    detailVC.title  = [self.dataArray[indexPath.row] objectForKey:@"title"];
    detailVC.burden = [self.dataArray[indexPath.row] objectForKey:@"burden"];
    detailVC.stepsArray = [self.dataArray[indexPath.row] objectForKey:@"steps"];
    detailVC.ingredients = [self.dataArray[indexPath.row] objectForKey:@"ingredients"];
    //下载图片
    ImageDownLoader *imageDown = [[ImageDownLoader alloc] init];
    [imageDown requestImageUrl:self.urlDetailArray[indexPath.row] successBlock:^(NSData *data) {
        detailVC.image = [UIImage imageWithData:data];
    } errorBlock:^(NSError *error) {
        
    }];
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"reuseIdentifier";
    
    self.cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!self.cell) {
        self.cell = [[FoodNameTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    //设置cell文本、背景图片
    self.cell.Show_MessageLabel.text = [self.dataArray[indexPath.row] objectForKey:@"title"];
    self.cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"foodDetail1_background.jpg"]];
    self.cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    [self.cell.TieleView sd_setImageWithURL:self.urlDetailArray[indexPath.row]];
    self.cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return self.cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
